/***********************************************************************************/
/* Code license:  GNU General Public License v3                                    */
/* Main page:     https://code.google.com/p/library-cross-platform/                */
/* Issue tracker: https://code.google.com/p/library-cross-platform/issues/list     */
/***********************************************************************************/

#ifndef __VALUE_2_STRING___
#define __VALUE_2_STRING___

#include <string>
#include <sstream>
#include <typeinfo>
#include <stdlib.h>
#include <math.h>

template<typename T>
std::string xdouble2string(T v, int accuracy)
{
  std::string       ret_str;
  std::stringstream quot_s;
  std::stringstream rem_s;
  long              quot_val  = (long)v;
  long long         rem_value = 0;
  T                 tmp_val   = (-1*((T)quot_val - v));

/*calculate digits of rem */
  while(accuracy--) {
   tmp_val *= 10.;
  }
  rem_value = (long long)tmp_val;
  quot_s << quot_val;
  rem_s << rem_value;
  ret_str = quot_s.str() + "." + rem_s.str();
  return ret_str;
}


template<typename T>
std::string tostring(T val, int accuracy = 10)
{
  std::string ret_str;
  if(typeid(T) == typeid(double)        ||
     typeid(T) == typeid(long double) ) {
    ret_str = xdouble2string<T>(val,accuracy);
  } else {
    std::stringstream ss;
    ss << val;
    ret_str = ss.str();
  }
  return ret_str;
}



#endif //__VALUE_2_STRING___
